﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KeyboardImageViewer
{
    /// <summary>
    /// Interaction logic for EditTagDependencies.xaml
    /// </summary>
    public partial class EditTagDependencies : Window
    {
        private List<KeyValuePair<string, string>> dependencyList;
        private TagAutoCompleteTextBox tagBox;
        private TagAutoCompleteTextBox dependentTagBox;

        public EditTagDependencies(MainWindow window)
        {
            this.Owner = window;

            InitializeComponent();

            Dictionary<string, int> tagsSummary = Database.GetTagsSummary(ImageManager.Connection);
            this.tagBox = new TagAutoCompleteTextBox
            {
                Margin = new Thickness(106, 5, 169, 0),
                VerticalAlignment = VerticalAlignment.Top,
                TagsSummary = tagsSummary,
                LimitToOneTag = true
            };

            this.dependentTagBox = new TagAutoCompleteTextBox
            {
                Margin = new Thickness(106, 31, 169, 0),
                VerticalAlignment = VerticalAlignment.Top,
                TagsSummary = tagsSummary,
                LimitToOneTag = true
            };

            this.mainGrid.Children.Add(this.tagBox);
            this.mainGrid.Children.Add(this.dependentTagBox);

            ImageManager.Config.ApplyWindowSize("TagDependencies", this);

            this.dependencyList = new List<KeyValuePair<string, string>>();
            this.dependencyList.AddRange(ImageManager.DependencyList);

            foreach (KeyValuePair<string, string> dependency in ImageManager.DependencyList)
            {
                this.dependencyListBox.Items.Add(dependency.Key + " -> " + dependency.Value);
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            string firstTag = TagOperations.GetFirstTag(this.tagBox.Text);
            string firstDependentTag = TagOperations.GetFirstTag(this.dependentTagBox.Text);

            if (firstTag == null || firstDependentTag == null)
            {
                MessageBox.Show("Cannot add dependency, fields must contain valid tags.");
                return;
            }

            this.dependencyList.Add(new KeyValuePair<string, string>(firstTag, firstDependentTag));
            this.dependencyListBox.Items.Add(firstTag + " -> " + firstDependentTag);

            this.tagBox.Text = "";
            this.dependentTagBox.Text = "";
        }

        private void removeButton_Click(object sender, RoutedEventArgs e)
        {
            int removeIndex = this.dependencyListBox.SelectedIndex;

            if (removeIndex >= 0)
            {
                this.dependencyList.RemoveAt(this.dependencyListBox.SelectedIndex);
                this.dependencyListBox.Items.RemoveAt(this.dependencyListBox.SelectedIndex);
            }
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            ImageManager.DependencyList = this.dependencyList;
            this.DialogResult = true;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ImageManager.Config.SaveWindowSize("TagDependencies", this);
        }
    }
}
